package com.pro_one.controller;

import com.pro_one.constant.MessageConstant;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.entity.Result;
import com.pro_one.pojo.Member;
import com.pro_one.service.MemberService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;

//会员管理
@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    private MemberService memberService;
    //会员分页
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = memberService.pageQuery(queryPageBean);


        return pageResult;
    }

    //会员新增
    @RequestMapping("/add")
    public Result add(@RequestBody Member member){
        try{
            memberService.add(member);
        }catch (Exception e){
            e.printStackTrace();
            //进入了catch就调用失败
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
        //成功
        return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);
    }

    //根据会员id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            //编辑成功
            Member member = memberService.findById(id);
            return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS,member);
        }catch (Exception e){
            e.printStackTrace();
            //编辑失败
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
    }
    //编辑检查项
    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member){
        try {
            memberService.edit(member);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
    }

    //删除会员信息
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            memberService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
    }
    @RequestMapping("/downup")
    public void downup(HttpServletRequest request, HttpServletResponse response, String[] memberIds) {
        try {

            //获得Excel模板文件绝对路径
            String temlateRealPath = request.getSession().getServletContext().getRealPath("template") +
                    File.separator + "downup.xlsx";
            //读取模板文件创建Excel表格对象
            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(temlateRealPath)));

            List<Map<String, Object>> list = memberService.findByIds(memberIds);
            XSSFSheet sheet = workbook.getSheetAt(0);

            Integer count = 7;

            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> map = list.get(i);
                Member member =(Member) map.get("member");
                List checkgroups =(List) map.get("checkgroups");
                List checkitems =(List) map.get("checkitems");
                List adderss =(List) map.get("adderss");
                count++;
                XSSFRow row = sheet.createRow(count);

                if(null==member){
                    row.createCell(2).setCellValue("");
                    row.createCell(3).setCellValue("");
                    row.createCell(4).setCellValue("");
                }else {
                    if(null==member.getName()){
                        member.setName("");
                    }
                    if(null==member.getSex()){
                        member.setSex("");
                    }
                    if(null==member.getPhoneNumber()){
                        member.setPhoneNumber("");
                    }

                    row.createCell(2).setCellValue(member.getName());
                    row.createCell(3).setCellValue(member.getSex());
                    row.createCell(4).setCellValue(member.getPhoneNumber());

                }

                row.createCell(5).setCellValue(adderss.toString());//机构地址
                row.createCell(6).setCellValue(map.get("sname").toString());//套餐
                row.createCell(7).setCellValue(checkgroups.toString());//检查组
                row.createCell(8).setCellValue(checkitems.toString());//检查项
            }
            //通过输出流进行文件下载
            /*两个头一个流,通过响应对象获取字节输出流,往浏览器上写数据,并设置两个响应头,给浏览器指定文件类型*/
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=downup.xlsx");
            workbook.write(out);
            out.flush();
            out.close();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/findAllHealThmanagerName")
    public Result findAllHealThmanagerName() {
        try {
            List<Map<String,String>> mapList = memberService.findAllHealThmanagerName();
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS,mapList);
        } catch (Exception e) {
            e.getMessage();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
    }
}